<template>
  <div>
    <form-marker 
      :isEdit="isEditPage"
      :formData="list" 
      :rulesList="rules"
      :selectList="selectData"
      :eidtList="detailData"
      v-on:on-address="onchangeAddress"
      v-on:on-confirm="onSaveInfo" 
      v-on:on-cancel="onCancelInfo"></form-marker>
  </div>
</template>
<script>
import FormMarker from '@/components/createPage/createMarker'
import {isPhone} from '@/common/utils'
export default {
  components: {
    FormMarker
  },
  data () {
    let checkCode = (rule, value, callback) => {
      if (value && this.pageType === 'new') {
        this.$http.post(`/${this.$const.PREFIX.MAINDATA}/customers/check`, {storeNo: value}).then((res) => {
          if (res.result) {
            // this.noRepeat = res.data // res.data.data [false]有重复/【true】无重复
            if (!res.data) {
              callback(new Error('编码已存在，请重新输入'))
            } else {
              callback()
            }
          }
        })
      } else {
        callback()
      }
    }
    // 验证address
    let onCheckAddress = (rule, value, callback) => {
      if (this.isEditPage || (this.checkAddress && this.checkAddress.provinceName && this.checkAddress.cityName && this.checkAddress.countyName)) {
        callback()
      } else {
        // callback(new Error('省-市-县区-不允许为空'))
        callback(new Error('地址不允许为空'))
      }
    }
    // 验证联系电话
    let onCheckPhone = (rule, value, callback) => {
      if (!isPhone(value)) {
        callback(new Error('请输入正确格式的联系电话'))
      } else {
        callback()
      }
    }
    return {
      list: [
        {
          type: 'input',
          label: '门店编码',
          name: '门店编码',
          prop: 'storeNo',
          isDisable: true
        },
        {
          type: 'input',
          label: '门店名称',
          name: '门店编码',
          prop: 'name'
        },
        {
          type: 'selectProvince',
          label: '门店地址',
          name: '门店地址',
          prop: 'address',
          child: [
            {
              type: 'select',
              name: '省',
              prop: 'provinceName'
            },
            {
              type: 'select',
              name: '市',
              prop: 'cityName'
            },
            {
              type: 'select',
              name: '区县',
              prop: 'countyName'
            }
          ]
        },
        {
          type: 'input',
          name: '详细地址',
          prop: 'street'
        },
        {
          type: 'select',
          label: '所属代理商公司',
          name: '所属代理商公司',
          prop: 'businessCompanyCode',
          propName: 'businessCompanyName',
          optionLabel: 'name',
          optionValue: 'code'
        },
        {
          type: 'select',
          label: '所属区域',
          name: '所属区域',
          prop: 'areaCode',
          propName: 'areaName',
          optionLabel: 'name',
          optionValue: 'code'
        },
        {
          type: 'select',
          label: '发货主体',
          name: '发货主体',
          prop: 'deliverySubjectCode',
          propName: 'deliverySubjectName',
          optionLabel: 'name',
          optionValue: 'code'
        },
        {
          type: 'select',
          label: '默认发货仓库',
          name: '默认发货仓库',
          prop: 'deliveryWarehouseCode',
          propName: 'deliveryWarehouseName',
          optionLabel: 'name',
          optionValue: 'code'
        },
        {
          type: 'input',
          label: '联系人',
          name: '联系人',
          prop: 'linkman'
        },
        {
          type: 'input',
          label: '联系电话',
          name: '联系人',
          prop: 'mobile'
        }
      ],
      rules: {
        storeNo: [
          { max: 32, message: '长度不可超过32个字符', trigger: 'change' },
          { required: true, message: '门店编码不允许为空', trigger: 'change' },
          { required: true, validator: checkCode, trigger: 'change' }
        ],
        name: [
          { max: 64, message: '长度不可超过64个字符', trigger: 'blur' },
          { required: true, message: '门店名称不允许为空', trigger: 'blur' }
        ],
        address: [
          { required: true, validator: onCheckAddress, trigger: 'change' }
        ],
        street: [
          { required: true, message: '详细地址不允许为空', trigger: 'blur' },
          { max: 128, message: '长度不可超过128个字符', trigger: 'blur' }
        ],
        businessCompanyCode: [{ required: true, message: '所属公司不允许为空', trigger: 'change' }],
        areaCode: [{ required: true, message: '所属区域不允许为空', trigger: 'change' }],
        deliverySubjectCode: [{ required: true, message: '发送主体不允许为空', trigger: 'change' }],
        deliveryWarehouseCode: [{ required: true, message: '默认发送仓库不允许为空', trigger: 'change' }],
        linkman: [
          { max: 64, message: '长度不可超过64个字符', trigger: 'blur' },
          { required: true, message: '联系人不允许为空', trigger: 'blur' }
        ],
        mobile: [
          { max: 20, message: '长度不可超过20个字符', trigger: 'blur' },
          { required: true, message: '联系电话不允许为空', trigger: 'blur' },
          { required: true, validator: onCheckPhone, trigger: 'change' }
        ]
      },
      selectData: {},
      detailData: {},
      isEditPage: false,
      noRepeat: false,
      checkAddress: {}
    }
  },
  mounted () {
    this.pageType = this.$route.params.currentId
    this.getCompanySelect()
    this.getAreaSelect()
    this.getSubjectSelect()
    this.getWarehouseSelect()
    this.getDirectDetail()
  },
  methods: {
    // 获取company下拉列表数据
    async getCompanySelect () {
      let {data} = await this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/company/findCompanyList`)
      this.selectData.businessCompanyCode = data
      this.selectData = JSON.parse(JSON.stringify(this.selectData))
    },
    // 获取主体下拉列表数据
    async getSubjectSelect () {
      let {data} = await this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/company/findCompanyList`)
      this.selectData.deliverySubjectCode = data
      this.selectData = JSON.parse(JSON.stringify(this.selectData))
    },
    // 获取area下拉列表数据
    async getAreaSelect () {
      let {data} = await this.$http.post(`/${this.$const.PREFIX.MAINDATA}/mareas/list`, {})
      this.selectData.areaCode = data
      this.selectData = JSON.parse(JSON.stringify(this.selectData))
    },
    // 获取仓库下拉列表数据
    async getWarehouseSelect () {
      let {data} = await this.$http.post(`/${this.$const.PREFIX.MAINDATA}/mwarehouses/list`, {})
      this.selectData.deliveryWarehouseCode = data
      this.selectData = JSON.parse(JSON.stringify(this.selectData))
    },
    // 获取编辑详情数据
    async getDirectDetail () {
      if (this.pageType && this.pageType !== 'new') {
        let {data} = await this.$http.post(`/${this.$const.PREFIX.MAINDATA}/customers/getOne`, {id: this.pageType})
        this.detailData = data
        this.isEditPage = true
      }
    },
    onchangeAddress (val) {
      this.checkAddress = val
    },
    // 接口保存新建信息
    onSaveInfo (val) {
      let url = this.pageType === 'new' ? `/${this.$const.PREFIX.MAINDATA}/customers/save` : `/${this.$const.PREFIX.MAINDATA}/customers/update`
      let params = val
      params.type = 1 // 【0】直营门店 【1】代理商门店
      this.$http.post(url, params).then((res) => {
        if (res.result) {
          this.$message.success('操作成功')
          this.$router.push({name: 'agentStore'})
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    onCancelInfo () {
      this.$router.push({name: 'agentStore'})
    }
  }
}
</script>
<style lang="scss" scoped>

</style>


